package com.ncr.travel.appmgr.client.presenter;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.ncr.travel.appmgr.client.event.ConfigChangeEvent;
import com.ncr.travel.appmgr.client.event.ConfigChangeHandler;
import com.ncr.travel.appmgr.client.event.PreviewConfigChangeEvent;
import com.ncr.travel.appmgr.client.event.PreviewConfigChangeEventHandler;
import com.ncr.travel.appmgr.client.mvp.BasePresenter;
import com.ncr.travel.appmgr.client.mvp.EventBus;
import com.ncr.travel.appmgr.client.service.AppManagerWebService;
import com.ncr.travel.appmgr.client.view.MainCompositeView;
import com.ncr.travel.appmgr.model.shared.ApplicationConfiguration;
import com.ncr.travel.appmgr.model.shared.Product;
import com.ncr.travel.appmgr.model.shared.ScreenConfiguration;

public class MainWindowPresenterImpl extends BasePresenter<MainCompositeView<ScreenConfiguration>> implements MainWindowPresenter {

	private final AppManagerWebService webService;
	private ApplicationConfiguration applicationConfiguration;
	private CategoryAndScreenPresenter curentCategoryAndScreenPresenter;
	private ScreenConfiguration selectedScreenConfiguration;
	private Map<Integer, CategoryAndScreenPresenter> categoryScreenConfigurations;
	private final Provider<CategoryAndScreenPresenterImpl> categoryAndScreenPresenterProvider;

	@Inject
	public MainWindowPresenterImpl(AppManagerWebService webService, EventBus eventBus, MainCompositeView mainCompositeView, Provider<CategoryAndScreenPresenterImpl> categoryAndScreenPresenterProvider) {
		super(eventBus, mainCompositeView);
		System.out.println("Constructing MainWindowPresenter");
		this.webService = webService;
		this.categoryAndScreenPresenterProvider = categoryAndScreenPresenterProvider;
		view.setPresenter(this);
		bind();
	}

	@Override
	public void bind() {
		System.out.println("(MainWindowPresenter) Called bind()");
		registerHandler(eventBus.addHandler(ConfigChangeEvent.getType(), new ConfigChangeHandler() {
			public void onConfigChanged(ConfigChangeEvent event) {
				doUpdateScreenConfiguration(event.getScreenConfiguration());
			}
		}));
		registerHandler(eventBus.addHandler(PreviewConfigChangeEvent.getType(), new PreviewConfigChangeEventHandler() {
			public void onPreview(PreviewConfigChangeEvent event) {
				doPreviewConfigChanges();
			}
		}));
		registerHandler(view.getProductsTabPanel().addSelectionHandler(new SelectionHandler<Integer>() {
			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				doOnProductTabSelected(event.getSelectedItem());
			}
		}));
		/*registerHandler(view.getMainTabPanel().addSelectionHandler(new SelectionHandler<Integer>() {
			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				doOnMainTabSelected(event.getSelectedItem());
			}
		}));*/
	}

	private void doUpdateScreenConfiguration(ScreenConfiguration screenConfig) {
		if (screenConfig != null) {
			setSelectedScreenConfiguration(screenConfig);
			setPreviewURL();
		} else {
			resetSelectedScreenConfiguration();
			resetPreviewURL();
		}
	}

	private void doOnProductTabSelected(Integer selectedItem) {
		curentCategoryAndScreenPresenter = categoryScreenConfigurations.get(selectedItem);
	}

	@Override
	public void populateProductsTabPanel(ApplicationConfiguration applicationConfiguration) {
		System.out.println("(MainWindowPresenter) Called populateProductsTabPanel()");
		categoryScreenConfigurations = new HashMap<Integer, CategoryAndScreenPresenter>();
		TabLayoutPanel productsTabPanel = view.getProductsTabPanel();
		for (int i = 0, n = applicationConfiguration.getProducts().size(); i < n; i++) {
			Product product = applicationConfiguration.getProducts().get(i);
			CategoryAndScreenPresenterImpl categoryAndScreenPresenter = categoryAndScreenPresenterProvider.get();
			categoryAndScreenPresenter.setProduct(product);
			categoryAndScreenPresenter.go(view.getAppMgrTabPanel());

			categoryScreenConfigurations.put(i, categoryAndScreenPresenter);

			Label productsTabItemLabel = new Label(product.getName());
			 productsTabItemLabel.setStyleName("productsTabItemLabel");
	         if (i == 0) {
	             productsTabItemLabel.addStyleName("productsTabItemLabel-First");
	        }
	         
			productsTabItemLabel.setHeight("35px");
			productsTabItemLabel.setWordWrap(false);
			productsTabPanel.add(categoryAndScreenPresenter.getView().asWidget(), productsTabItemLabel);
		}
		productsTabPanel.selectTab(1);
	}

	private void doPreviewConfigChanges() {
		setPreviewURL();
	}

	private void setSelectedScreenConfiguration(ScreenConfiguration selectedScreenconfiguration) {
		this.selectedScreenConfiguration = selectedScreenconfiguration;
	}

	private void resetSelectedScreenConfiguration() {
		this.selectedScreenConfiguration = null;
	}

	private void setPreviewURL() {
		if (selectedScreenConfiguration != null && selectedScreenConfiguration.getUrl() != null && curentCategoryAndScreenPresenter != null) {
			curentCategoryAndScreenPresenter.setPreviewURL(selectedScreenConfiguration.getUrl());
		}
	}

	private void resetPreviewURL() {
		if (curentCategoryAndScreenPresenter != null) {
			curentCategoryAndScreenPresenter.setPreviewURL("");
		}
	}

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(view.asWidget());
		view.getAppMgrTabPanel().selectTab(1);
	}

}
